package com.akkainaction.cluster.wordcount.router.service;

import akka.actor.UntypedActor;
import akka.cluster.Cluster;
import com.akkainaction.cluster.wordcount.router.po.Article;
import com.akkainaction.cluster.wordcount.router.po.CountResult;

import static akka.cluster.ClusterEvent.MemberUp;

public class WordCountService extends UntypedActor {

    Cluster cluster = Cluster.get(getContext().system());

    @Override
    public void preStart() {
        cluster.subscribe(getSelf(), MemberUp.class);
    }

    @Override
    public void postStop() {
        cluster.unsubscribe(getSelf());
    }

    @Override
    public void onReceive(Object msg) throws Exception {
        if (msg instanceof Article) {
            System.out.println("当前节点:" + cluster.selfAddress() + ",self=" + getSelf() + "正在处理……");
            Article art = (Article) msg;
            String content = art.getContent();
            int word_count = content.split(" ").length;
            getSender().tell(new CountResult(art.getId(), word_count), getSelf());
        }  else {
            unhandled(msg);
        }
    }
}